package com.dusk.net
{
    import com.dusk.tool.SwfParser;
    import com.dusk.util.ArrayUtil;
    import com.dusk.util.SystemUtil;
    import com.hurlant.util.asn1.parser.printableString;
    
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.LoaderInfo;
    import flash.events.Event;
    import flash.media.Sound;
    import flash.system.ApplicationDomain;
    import flash.system.System;
    import flash.utils.ByteArray;
    
    public class AssetItem
    {
        public function AssetItem()
        {
            _classLink = [];
        }
        
        public var url:String;
        
        public var type:String;
        
        public var bytes:ByteArray;
        
        public var sound:Sound;
        
        public var bitmapData:BitmapData;
        
        public var json:Object;
        
        public var xml:XML;
        
        private var _classLink:Array;
        
        private var _loaderInfo:LoaderInfo;
        
        private var _buildCompleteCallback:Function;
        
        public function buildAsset(callBack:Function = null):void
        {
            _buildCompleteCallback = callBack;
            var loader:Loader;
            bytes.position = 0;
            switch (type)
            {
                case AssetType.IMAGE:
                    loader = new Loader();
                    _loaderInfo = loader.contentLoaderInfo;
                    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadToDomainComplete);
                    loader.loadBytes(bytes, SystemUtil.getLoaderContext());
                    break;
                case AssetType.SOUND:
                    var snd:Sound = new Sound();
                    snd.loadCompressedDataFromByteArray(bytes, bytes.length);
                    sound = snd;
                    buildComplete();
                    break;
                case AssetType.VIDEO:
                    //todo
                    buildComplete();
                    break;
                case AssetType.JSON:
                    json = JSON.parse(bytes.readUTFBytes(bytes.length));
                    buildComplete();
                    break;
                case AssetType.XML:
                    xml = new XML(bytes.readUTFBytes(bytes.length));
                    buildComplete();
                    break;
                case AssetType.SWF:
                    var parser:SwfParser = new SwfParser();
                    parser.parseSwf(bytes);
                    _classLink = parser.getSymbolClassLink();
                    loader = new Loader();
                    _loaderInfo = loader.contentLoaderInfo;
                    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadToDomainComplete);
                    loader.loadBytes(bytes, SystemUtil.getLoaderContext());
                    break;
                case AssetType.CONFIG:
                    json = parseConfigFile(bytes);
                    break;
                case AssetType.BYTES:
                default:
                    break;
            }
        }
        
        private function parseConfigFile(byte:ByteArray):Object
        {
            return null;
            //todo
        }
        
        private function onLoadToDomainComplete(e:Event):void
        {
            e.target.removeEventListener(Event.COMPLETE, onLoadToDomainComplete);
            switch (type)
            {
                case AssetType.SWF:
                    _loaderInfo.removeEventListener(Event.COMPLETE, onLoadToDomainComplete);
                    buildComplete();
                    break;
                case AssetType.IMAGE:
                    bitmapData = ((e.target as LoaderInfo).loader.content as Bitmap).bitmapData;
                    buildComplete();
                    break;
            }
        }
        
        private function buildComplete():void
        {
            if (_buildCompleteCallback)
            {
                _buildCompleteCallback();
            }
            if (type != AssetType.BYTES)
            {
                if (bytes) bytes.clear();
                bytes = null;
            }
        }
        
        public function getClass(linkKey:String):Object
        {
            return _loaderInfo.applicationDomain.getDefinition(linkKey);
        }
        
        public function getClassInstance(linkKey:String):Object
        {
            var cls:* = getClass(linkKey);
            return new cls();
        }
        
        public function hasClass(linkKey:String):Boolean
        {
            return ArrayUtil.contains(_classLink, linkKey);
        }
        
        public function destroy():void
        {
            if (bitmapData)
            {
                bitmapData.dispose();
            }
            if (bytes)
            {
                bytes.clear();
            }
            if (xml)
            {
                System.disposeXML(xml);
            }
            url = null;
            xml = null;
            type = null;
            json = null;
            bytes = null;
            sound = null;
            bitmapData = null;
            _buildCompleteCallback = null;
        }
    }
}
